﻿using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace GPSTest
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            gps.InvalidPositionReceived += gps_InvalidPositionReceived;
            gps.PositionReceived += gps_PositionReceived;
            gps.NMEASentenceReceived += gps_NMEASentenceReceived;

            Debug.Print("Program Started");
            Debug.Print("GPS enabled? " + gps.Enabled.ToString());            
        }

        void Alarm()
        {
            // up
            PlayNote(Tunes.Tone.C4, 200);
            PlayNote(Tunes.Tone.G4, 200);
            PlayNote(Tunes.Tone.C4, 200);
            PlayNote(Tunes.Tone.G4, 200);

            tunes.Play();
        }

        void PlayNote(Tunes.Tone tone, int duration)
        {
            Tunes.MusicNote note = new Tunes.MusicNote(tone, duration);

            tunes.AddNote(note);
        }

        void gps_InvalidPositionReceived(GTM.Seeed.GPS sender)
        {
            Debug.Print("invalid position received");
        }

        void gps_PositionReceived(GTM.Seeed.GPS sender, GTM.Seeed.GPS.Position position)
        {
            Debug.Print("Latitude: " + position.Latitude.ToString());
            Debug.Print("Longitude: " + position.Longitude.ToString());
            Debug.Print("Last Valid Age: " + gps.LastValidPositionAge.ToString());
            this.Alarm();
        }

        void gps_NMEASentenceReceived(GTM.Seeed.GPS sender, string nmeaSentence)
        {
            Debug.Print("NMEA Sentence: " + nmeaSentence);
        }
    }
}
